﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfRcp.Core.Services;
using Microsoft.Practices.Composite.Logging;
using System.Diagnostics;
using log4net;
using Microsoft.Practices.ServiceLocation;
using log4net.Appender;

namespace WpfRcp.Logging.Log4Net
{
    public class Log4NetLoggerService : ILoggerService
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(Log4NetLoggerService));

        public void Log(string message, string category, TraceEventType traceEventType, string title)
        {
            DoLog(message, ParseTraceEventType(traceEventType));
        }

        public void Log(string message, Category category, Priority priority)
        {
            DoLog(message, category);
        }

        private void DoLog(string message, Category category)
        {
            switch (category)
            {
                case Category.Debug:
                    {
                        Logger.Debug(message);
                        break;
                    }
                case Category.Exception:
                    {
                        Logger.Error(message);
                        break;
                    }
                case Category.Info:
                    {
                        Logger.Info(message);
                        break;
                    }
                case Category.Warn:
                    {
                        Logger.Warn(message);
                        break;
                    }
                default:
                    break;
            }
        }

        private static Category ParseTraceEventType(TraceEventType traceEventType)
        {
            switch (traceEventType)
            {
                case TraceEventType.Critical:
                case TraceEventType.Error:
                    {
                        return Category.Exception;
                    }
                case TraceEventType.Information:
                case TraceEventType.Resume:
                case TraceEventType.Start:
                case TraceEventType.Stop:
                case TraceEventType.Suspend:
                case TraceEventType.Transfer:
                    {
                        return Category.Info;
                    }
                case TraceEventType.Verbose:
                    {
                        return Category.Debug;
                    }
                case TraceEventType.Warning:
                    {
                        return Category.Warn;
                    }
                default:
                    {
                        return Category.Debug;
                    }
            }
        }
    }
}
